import ChannelBase from "../base/ChannelBase";
import ChannelData from "../data/ChannelData";
import ChannelManager from "../manager/ChannelManager";

export default class BaiduChannel extends ChannelBase {
    /**
     * 初始化渠道
     */
    protected InitChannel() {
        this.is_has_ad = true;
        this.banner_height = 200;

        if (this.is_has_ad) {
            this.CreateVideo();
        }
    }

    //#region 广告相关
    /**
     * 创建插屏广告
     */
    public CreatePage() {

    }
    /**
     * 展示插屏广告
     */
    public ShowPage(on_successed: Function, on_failed: Function) {

    }

    /**
     * 创建视频广告
     */
    public CreateVideo() {
        this.video_obj = swan.createRewardedVideoAd({
            appSid: this.appid,
            adUnitId: this.video_id_list[0]
        });
        this.video_obj.load();
        this.video_obj.onClose(res => {
            if (res.isEnded) {
                if (this.video_successed != null && this.video_successed != undefined) {
                    this.video_successed();
                }
            } else {
                if (this.video_failed != null && this.video_failed != undefined) {
                    this.video_failed();
                }
            }
        });
        this.video_obj.onError(res => {
            if (this.video_failed != null && this.video_failed != undefined) {
                this.video_failed();
            }
            console.error(res.errMsg + '----' + res.errCode);
        });
    }


    /**
     * 展示视频广告
     */
    public ShowVideo(on_successed: Function, on_failed: Function) {
        cc.audioEngine.pauseAll();
        this.video_successed = on_successed;
        this.video_failed = on_failed;
        this.video_obj.show();
        this.HideBanner();
    }

    /**
     * 展示banner广告
     */
    public ShowBanner(on_successed: Function, on_failed: Function) {
        let sysInfo = swan.getSystemInfoSync();
        let width = sysInfo.windowWidth;
        let height = sysInfo.windowHeight;

        this.banner_obj = swan.createBannerAd({
            appSid: this.appid,
            adUnitId: this.banner_id_list[0],
            style: {
                left: width * 0.5 - 300 * 0.5,
                top: height - 75,
                width: 300,
            }
        });

        this.banner_obj.onLoad(() => {
            if (on_successed) on_successed();
            this.banner_obj.show();
        });

        this.banner_obj.onError(res => {
            console.error(res.errMsg + '----' + res.errCode);
            if (on_failed) on_failed();
        });
    }
    /**
     * 隐藏banner广告
     */
    public HideBanner() {
        if (this.banner_obj != null && this.banner_obj != undefined) {
            this.banner_obj.destroy();
            this.banner_obj = null;
        }
    }

    /**
     * 创建盒子广告
     */
    public CreateBox() { }
    /**
     * 显示盒子广告
     */
    public ShowBox() { }
    /**
     * 隐藏盒子广告
     */
    public HideBox() { }

    /**
     * 创建积木广告
     */
    public CreateJuggle() { }
    /**
     * 显示积木广告
     */
    public ShowJuggle() { }
    /**
     * 隐藏积木广告
     */
    public HideJuggle() { }

    /**
     * 创建原生广告
     */
    public CreateNative() { }
    /**
    * 展示原生广告
    */
    public ShowNative(on_successed: Function, on_failed: Function) { }
    /**
    * 隐藏原生广告
    */
    public HideNative() { }
    //#endregion 广告相关

    //#region 分享/录屏相关 
    /**
     * 分享游戏
     */
    public ShareGame() {
        if (swan.openShare) {
            swan.openShare({
                title: ChannelData.record_topics_1,
                content: ChannelData.share_title,
                imageUrl: ChannelData.share_image_url,
                success: res => {
                    console.log('share success', res);
                },
                fail: err => {
                    if (err.errCode == 904) {
                        ChannelManager.GetInstance().ShowToast("客户端版本过低");
                    } else
                        console.log(err);
                }
            });
        } else {
            ChannelManager.GetInstance().ShowToast("客户端版本过低");
        }
    }

    /**
     * 开始录屏
     */
    public StartRecord() { }
    /**
     * 停止录屏
     */
    public StopRecord(on_stop: Function) { }
    /**
     * 分享录屏
     */
    public ShareRecord(on_successed: Function, on_failed: Function) { }
    //#endregion 分享/录屏相关
    /*
     * 获取设备信息
     */
    public GetSystemInfo() {return qg.getSystemInfo({});}
    /*
     * 短震动
     */
    public VibrateShort() { }
    /*
     * 长震动
     */
    public VibrateLong() { }

    /**
     * 打开另一个小程序
     * @param target 目标小程序appid
     * @param path 打开的页面路径
     * @param extraData 需要传递给目标小程序的数据
     * @param ON_SUCCESS 成功回调,返回参数res
     * @param ON_FAILED 失败回调,返回参数res
     * @param is_developer envVersion 的合法值是否为 develop(开发版)
     */
    public NavigateToMiniProgram(target: string, path: string, extraData: object, ON_SUCCESS: Function, ON_FAILED: Function, is_developer = false) {

    }

    //#region 生命周期
    public OnShow(callback: Function) {
        swan.onShow(callback);
    }
    public OnHide(callback: Function) {
        swan.onHide(callback);
    }
    public OffShow(callback: Function) {
        swan.offShow(callback);
    }
    public OffHide(callback: Function) {
        swan.offHide(callback);
    }
    public GetLaunchOptionsSync(): any {
        return swan.getLaunchOptionsSync();
    }
    public GetEnterOptionsSync(): any {
        return swan.getEnterOptionsSync();
    }
    public ExitMiniProgram(on_successed: Function, on_failed: Function, on_complete: Function) {
        swan.exitMiniProgram(on_successed, on_failed, on_complete);
    }
    //#endregion 生命周期
}
